package cn.miao.photoprinter.util;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import cn.miao.photoprinter.R;

@SuppressLint("InflateParams")
public class DialogUtil {

	public static Dialog getCustomDialog(Activity context) {
		final Dialog dialog = new Dialog(context, R.style.Dialog);
		return dialog;
	}

	/**
	 * 非activity的context获取自定义对话框
	 * 
	 * @param context
	 * @return
	 */
	public static Dialog getWinDialog(Context context) {
		final Dialog dialog = new Dialog(context, R.style.Dialog);
		dialog.getWindow()
				.setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
		return dialog;
	}

	public static int getScreenWidth(Activity context) {
		DisplayMetrics dm = new DisplayMetrics();
		context.getWindowManager().getDefaultDisplay().getMetrics(dm);
		return dm.widthPixels;
	}

	public static int getScreenHeight(Activity context) {
		DisplayMetrics dm = new DisplayMetrics();
		context.getWindowManager().getDefaultDisplay().getMetrics(dm);
		return dm.heightPixels;
	}

	/**
	 * confirm 窗口的监听
	 * 
	 * @param context
	 * @param content
	 * @param listener
	 * @return Dialog
	 * @since v 1.0
	 */
	public static Dialog getConfirmDialog(Activity context, String content,
			View.OnClickListener listener) {
		final Dialog dlg = new Dialog(context, R.style.Dialog);
		View layout = LayoutInflater.from(context).inflate(
				R.layout.dialog_confirm, null);
		TextView tvContent = (TextView) layout.findViewById(R.id.dialogtext);
		Button btYes = (Button) layout.findViewById(R.id.dialogyes);
		Button btNo = (Button) layout.findViewById(R.id.dialogno);
		tvContent.setText(content);
		btNo.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				dlg.dismiss();
			}
		});
		btYes.setOnClickListener(listener);
		dlg.setContentView(layout);
		return dlg;
	}

	/**
	 * alert 窗口的监听
	 * 
	 * @param context
	 * @param content
	 * @param listener
	 * @return Dialog
	 * @since v 1.0
	 */
	public static Dialog getAlertDialog(Activity context, String content,
			View.OnClickListener listener) {
		final Dialog dlg = new Dialog(context, R.style.Dialog);
		View layout = LayoutInflater.from(context).inflate(
				R.layout.dialog_alert, null);
		TextView tvContent = (TextView) layout.findViewById(R.id.dialogtext);
		Button btYes = (Button) layout.findViewById(R.id.dialogyes);
		tvContent.setText(content);
		if (null == listener) {
			btYes.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					dlg.dismiss();
				}
			});
		} else {
			btYes.setOnClickListener(listener);
		}
		dlg.setContentView(layout);
		return dlg;
	}

	/**
	 * 获取density
	 * 
	 * @return float
	 */
	public static float getDensity(Activity context) {
		DisplayMetrics metric = new DisplayMetrics();
		context.getWindowManager().getDefaultDisplay().getMetrics(metric);
		return metric.density; // 屏幕宽度（像素）
	}

}